package com.gwt.auth.controller;

import com.gwt.auth.api.AuthApi;
import com.gwt.auth.params.TokenForm;
import com.gwt.auth.service.AuthService;
import com.gwt.auth.vo.AuthTokenVO;
import com.gwt.auth.vo.AuthUser;
import com.gwt.cloud.result.ApiResult;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class AuthController implements AuthApi {

    @Resource
    private AuthService authService;

    @Override
    public ApiResult<AuthTokenVO> tokenCreate(TokenForm form) {
        AuthTokenVO authTokenVO = new AuthTokenVO();
        authTokenVO.setUserId(form.getUserId());
        AuthUser authUser = authService.userLogin(form.getUserId());
        authTokenVO.setToken(authUser.getToken());
        return ApiResult.ok(authTokenVO);
    }

    @Override
    public ApiResult<AuthTokenVO> tokenValidate() {
        AuthUser authUser = authService.tokenValidate();
        AuthTokenVO authTokenVO = new AuthTokenVO();
        authTokenVO.setUserId(authUser.getUserId());
        authTokenVO.setToken(authUser.getToken());
        return ApiResult.ok(authTokenVO);
    }

    @Override
    public ApiResult<AuthTokenVO> tokenRefresh(TokenForm form) {
        AuthUser authUser = authService.tokenRefresh();
        AuthTokenVO authTokenVO = new AuthTokenVO();
        authTokenVO.setUserId(form.getUserId());
        authTokenVO.setToken(authUser.getToken());
        return ApiResult.ok(authTokenVO);
    }

    @Override
    public ApiResult tokenClean() {
        authService.loginOut();
        return ApiResult.ok();
    }
}
